//
//  HJChatMessageNoticeCell.m
//  KaiChat
//
//  Created by lx on 2023/11/14.
//

#import "HJChatMessageNoticeCell.h"
#import "HJChatMessageNoticeLayout.h"
#import "HJChatMessageModel.h"
#import "HJGradientView.h"
#import "QMUIButton.h"

@interface HJChatMessageNoticeCell()

@property (nonatomic, strong) HJChatMessageNoticeLayout *noticeLayout;
@property (nonatomic, weak) UILabel *contentLabel;
@property (nonatomic, weak) HJGradientView *titleView;
@property (nonatomic, weak) UIImageView *noticeIconView;
@property (nonatomic, weak) UILabel *noticeLabel;
@property (nonatomic, weak) QMUIButton *foldButton;

@end


@implementation HJChatMessageNoticeCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        HJGradientView *titleView = [HJGradientView addToView:self.bubbleView];
        titleView.colors = @[UIColorMake(0xA0B1FF), UIColorMake(0xFFD4E4)];
        [titleView setStartPoint:CGPointZero endPoint:CGPointMake(1, 0)];
        _titleView = titleView;
        
        UIImageView *noticeIconView = [UIImageView addToView:titleView];
        noticeIconView.image = [UIImage imageNamed:@"notice_icon"];
  
        UILabel *noticeLabel = [UILabel lableWithSuperView:titleView font:UIFontMediumMake(14) color:UIColor.whiteColor title:@"Group notice" textAlignment:NSTextAlignmentLeft];
        
        UILabel *contentLabel = [UILabel addToView:self.bubbleView];
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.textColor = UIColor.tj_333333Color;
        contentLabel.numberOfLines = 0;
        
        QMUIButton *foldButton = [QMUIButton buttonWithSuperView:self.bubbleView font:UIFontMake(14) color:UIColor.tj_mainColor title:nil];
        [foldButton setImagePosition:QMUIButtonImagePositionRight];
        [foldButton setImage:[UIImage imageNamed:@"message_top"] forState:UIControlStateNormal];
        [foldButton setImage:[UIImage imageNamed:@"message_down"] forState:UIControlStateSelected];
        [foldButton setTitle:@"Hide" forState:UIControlStateNormal];
        [foldButton setTitle:@"Expand" forState:UIControlStateSelected];
        [foldButton setSpacingBetweenImageAndTitle:4];
        [foldButton addTarget:self action:@selector(didClickFoldButton:) forControlEvents:UIControlEventTouchUpInside];
        _foldButton = foldButton;
        
        _contentLabel = contentLabel;
        _noticeLabel = noticeLabel;
        _noticeIconView = noticeIconView;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _titleView.frame = self.noticeLayout.titleViewRect;
    _contentLabel.frame = self.noticeLayout.contentRect;
    _noticeIconView.frame = CGRectMake(self.noticeLayout.contentRect.origin.x, (self.noticeLayout.titleViewRect.size.height - _noticeIconView.image.size.height) * 0.5, _noticeIconView.image.size.width,  _noticeIconView.image.size.height);
    _noticeLabel.frame = CGRectMake(_noticeIconView.tj_MaxX + 6, 0, self.noticeLayout.titleViewRect.size.width - _noticeIconView.tj_MaxX - 6, self.noticeLayout.titleViewRect.size.height);
    
    if (self.noticeLayout.foldRect.size.height > 0) {
        [_foldButton sizeToFit];
        _foldButton.frame = CGRectMake(self.noticeLayout.contentRect.origin.x, CGRectGetMaxY(self.noticeLayout.contentRect), _foldButton.bounds.size.width, self.noticeLayout.foldRect.size.height);
    }
}

- (void)setMessageLayout:(HJChatMessageLayout *)messageLayout {
    [super setMessageLayout:messageLayout];
    
    self.noticeLayout = (HJChatMessageNoticeLayout *)messageLayout;
    self.contentLabel.font = self.noticeLayout.textFont;
    self.contentLabel.attributedText = self.noticeLayout.attributedString;
    
    if (self.noticeLayout.foldRect.size.height) {
        _foldButton.hidden = NO;
        if (self.noticeLayout.isFold) {//显示Expand
            _foldButton.selected = YES;
        } else {//显示Hide
            _foldButton.selected = NO;
        }
    } else {
        _foldButton.hidden = YES;
    }
}

- (void)didClickFoldButton:(QMUIButton *)button {
    if ([self.delegate respondsToSelector:@selector(chatMessageCell:didClickFoldButton:)]) {
        [self.delegate chatMessageCell:self didClickFoldButton:button];
    }
    
}


@end
